<template>
  <el-dialog v-model="visible" :title="title" width="920" show-footer show-zoom resize @close="close" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <template #default>
      <el-form ref="formRef" :rules="rules" :model="form" label-align="right" label-width="120" size="default">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="头程单号" prop="head_goods_order_no" v-if="handleType != 'add'">
              {{ form.head_goods_order_no }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="箱号" prop="head_box_order_no" v-if="handleType != 'add'">
              {{ form.head_box_order_no }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <vxe-table :data="form.detail" :round="true" show-overflow :column-config="{resizable: true}" :row-config="{height: 100}" height="500">
        <vxe-column title="SKU">
          <template #default="{ row }">
            {{ row.sku }}
          </template>
        </vxe-column>
        <vxe-column field="name" title="图片">
          <template #default="{ row }">
            <div>
              <div class="image-item" v-for="(item,index) in row.image" :key="index"><img :src="item"></div>
            </div>
          </template>
        </vxe-column>
        <vxe-column field="num" title="装箱数量">
          <template #default="{ row }">
            {{ row.num || '--' }}
          </template>
        </vxe-column>
        <vxe-column title="修改数量" v-if="handleType == 'edit'">
          <template #default="{ row }">
            <el-input v-model="row.modify_num" placeholder="修改数量" />
          </template>
        </vxe-column>
        <vxe-column field="opearn" title="操作" width="100" v-if="handleType == 'edit'">
          <template #default="{ row, rowIndex }">
            <el-link size="small" type="primary" @click="deleteRow(rowIndex)">删除</el-link>
          </template>
        </vxe-column>
      </vxe-table>
    </template>

    <template #footer>
      <template v-if="handleType == 'view'">
        <div class="flex justify-center py-2">
          <el-button type="" @click="close">关 闭</el-button>
        </div>
      </template>
      <div class="flex justify-center py-2" v-else>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="save(formRef)">保 存</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
import { getHeadOrderDetail, updateHeadOrder } from '../api/index'
import { getSKULocation } from '@/api/public'
import { reactive, ref, toRefs } from 'vue'
import { ElMessage } from 'element-plus'

const visible = ref(false)
const loading = ref(false)
const formRef = ref(null)
const emits = defineEmits(['query'])

const props = defineProps({
  wareDataList: {
    type: Array,
    default: [],
  },
})

const data = reactive({
  form: {},
  title: '',
  handleType: null,
})
const { form, title, handleType, rules } = toRefs(data)

const statusMap = new Map([
  ['edit', '头程装箱修改'],
  ['view', '头程装箱详情'],
])

const open = (type, id) => {
  title.value = statusMap.get(type)
  handleType.value = type
  visible.value = true
  if (id) getDetail(id)
}

const initForm = () => {
  form.value = getForm()
}
// 初始化
const getForm = () => {
  return {
    detail: []
  }
}

onMounted(() => {
  initForm()
})

const getDetail = async (id) => {
  try {
    const res = await getHeadOrderDetail({
      id
    })
    form.value = res.data
  } catch (e) {
    throw Error(e)
  }
}


const deleteRow = (index) => {
  if (form.value.detail.length <= 1) return
  form.value.detail.splice(index, 1)
}

const save = () => {
  if (!formRef.value) return
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      const detail = form.value.detail.map(i=>{
        return{
          id: i.id,
          num: i.modify_num,
        }
      })
      const data = {
        id: form.value.id,
        detail
      }
      if (form.value.id) {
        updateHeadOrder(data)
          .then((res) => {
            ElMessage.success(res.message)
            emits('query')
            close()
          })
          .finally((e) => {
            loading.value = false
          })
      }
    }
  })
}

const close = () => {
  initForm()
  visible.value = false
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
.borderNone {
  :deep(.el-input__wrapper) {
    border: none;
    box-shadow: none
  }
}
.image-item{
  width: 60px;
  height: 60px;
}
</style>
